package com.shengyin.ec.controller.account;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.City;
import com.shengyin.ec.model.entity.District;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.model.entity.Province;
import com.shengyin.ec.model.entity.Receiver;
import com.shengyin.ec.service.account.CityService;
import com.shengyin.ec.service.account.DistrictService;
import com.shengyin.ec.service.account.ProvinceService;
import com.shengyin.ec.service.account.ReceiverService;
import com.shengyin.ec.service.delivery.DelieveryService;
import com.shengyin.ec.service.payment.PaymentService;

@Controller
public class ReceiverController {
	
	@Autowired
	ReceiverService receiverService;
	
	@Autowired
	DelieveryService deliveryService;
	
	@Autowired
	PaymentService paymentService;
	
	@Autowired
	DistrictService districtService;
	
	@Autowired
	CityService cityService;

	@Autowired
	ProvinceService provinceService;
	
	@RequestMapping("/confirmReceiverInfo.do")
	public String confirmReceiverInfo(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Personal personal = (Personal)session.getAttribute("user");
		System.out.println("=================="+personal.getUsername());
		List<Receiver> receiver = receiverService.getReceiverListById(personal.getId());
		System.out.println("=================="+receiver.size());
		model.addAttribute("rec", receiver.get(0));
		System.out.println("=================="+receiver.get(0).getBetterDelieverTime());
		System.out.println("=================="+receiver.get(0).getRecieverName());
		
		return "/view/order/receiverInfo";
	}
	
	
	@RequestMapping("/toSetDefaultAddress.do")
	public String toSetDefaultAddress(HttpServletRequest request, Model model) {
		
		return "/view/order/receiverInfo";
	}
	
	//add by lcl 2012-7-25
	@RequestMapping(value = "/getCitysByProvinceId.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getCitysByProvinceId(
			@RequestParam("parentId") int parentId) {
		ModelMap modelMap = new ModelMap();
		List<City> city = cityService.getCityListByProvinceId(parentId);
		modelMap.addAttribute("city", city);
		
		return modelMap;
	}
	
	
	@RequestMapping(value = "/getDistrictByCityId.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getDistrictByCityId(
			@RequestParam("parentId") int parentId) {
		ModelMap modelMap = new ModelMap();
		List<District> district = districtService.getDistrictListByCityId(parentId);
		modelMap.addAttribute("district", district);
		
		return modelMap;
	}
	
	@RequestMapping("/toAddUserReceiver.do")
	public String toAddUserReceiver(HttpServletRequest request, Model model) {
		List<Province> provinceList = provinceService.getProvinceList();
		model.addAttribute("provinceList", provinceList);
		
		return "/view/user/addMemberReceiver";
	}
	
	@RequestMapping("/toReceiverList.do")
	public String toReceiverList(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Personal  personal = (Personal)session.getAttribute("user");
		List<Receiver> recerverList = receiverService.getReceiverListById(personal.getId());
		model.addAttribute("recerverList", recerverList);
		return "/view/user/memberReceiverList";
	}
	@RequestMapping("deleteReceiver.do")
	public String deleteReceiver(HttpServletRequest request, ReceiverForm receiverForm,Model model) {
		long id = receiverForm.getId();
		receiverService.deleteReceiver(receiverService.getReceiver(id));
		return "redirect:/toReceiverList.do";
	}
	@RequestMapping("/toModifyUserReceiver.do")
	public String toModifyUserReceiver(HttpServletRequest request,ReceiverForm receiverForm, Model model) {
		Receiver receiver = receiverService.getReceiver(receiverForm.getId());
		model.addAttribute("receiver", receiver);
		return "/view/user/modifyMemberReceiver";
	}
	@RequestMapping("/modifyUserReceiver.do")
	public String modifyUserReceiver(HttpServletRequest request, ReceiverForm receiverForm, Model model) {
		HttpSession session = request.getSession();
		Personal user = (Personal)session.getAttribute("user");
		//??????????,?????
		Receiver receiver = receiverService.getReceiver(receiverForm.getId());
		receiver.setBetterDelieverTime(receiverForm.getBetterDelieverTime());
		receiver.setCity(receiverForm.getCity());
		receiver.setDetailAddress(receiverForm.getDetailAddress());
		receiver.setDistrict(receiverForm.getDistrict());
		//receiver.setEmail(receiverForm.getEmail());
		receiver.setIsDefaultAddress(receiverForm.getIsDefaultAddress());
		receiver.setMobile(receiverForm.getMobile());
		receiver.setPostalCode(receiverForm.getPostalCode());
		receiver.setProvince(receiverForm.getProvince());
		receiver.setRecieverName(receiverForm.getRecieverName());
		receiver.setTelePhone(receiverForm.getTelePhone());
		if("1".equals(receiverForm.getIsDefaultAddress())){
			receiverService.setDefaultReceiver(user.getId(), receiver.getId());
		}
		//??????
		receiverService.updateReceiver(receiver);
		return "redirect:/toReceiverList.do";
	}
	//add by lcl 2012-10-10
    @RequestMapping("/addReceiver.do")
	 public String addReceiver(HttpServletRequest request, ReceiverForm receiverForm, Model model) {
    	Receiver receiver = new Receiver();
    	HttpSession session = request.getSession();
		Personal user = (Personal)session.getAttribute("user");
		String receiveType=request.getParameter("receiveType");
		
		try {
			
			BeanUtils.copyProperties(receiver, receiverForm);
			if(user==null)
				return "redirect:/toIndex.do";
			receiver.setUserId(user.getId());
			receiverService.createReceiver(receiver);
			if(receiveType!=null&&"1".equals(receiveType)){
				receiverService.setDefaultReceiver(user.getId(), receiver.getId());
				return "redirect:/view/toAccountingCenter.do";
			}else{
				if("1".equals(receiver.getIsDefaultAddress())){
					receiverService.setDefaultReceiver(user.getId(), receiver.getId());
				}
				return "redirect:/toReceiverList.do";
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	
		return "redirect:/toReceiverList.do";
	  	}
	
    @ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  	
}